1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4 using UnityEngine.UI;
5 using System.Diagnostics;
6
7 public class GameManager : Singleton<GameManager> {
8 [SerializeField]
9 private Material pieceP1;
10 [SerializeField]
11 private Material pieceP2;
12 [SerializeField]
13 private Grid grid;
14 [SerializeField]
15 private Camera mainCamera;
16 [SerializeField]
17 private LayerMask clickableMask;
18 [SerializeField]
19 private Color pieceHighlightColor;
20 [SerializeField]
21 private Material highlightMoveMaterial;
22 [SerializeField]
23 private Material highlightEatMaterial;
24 [SerializeField]
25 private Material highlightCheckMaterial;
26
27 public Text winnerText;
28 public Text blackScoreText;
29 public Text whiteScoreText;
30 public Text gamesPlayedText;
31 public GameObject winnerCanvas;
32 public GameObject continueButton;
33 public GameObject winnerButton;
34
35 public const string PLAYER_WHITE = "PLAYER_WHITE";
36 public const string PLAYER_BLACK = "PLAYER_BLACK";
37 public const string SCORE_MAX = "SCORE_MAX";
38 public const string GAME_MAX = "GAME_MAX";
39 public const string GAME_CURRENT = "GAME_CURRENT";
40 public const string CAMERA_VIEW = "CAMERA_VIEW";
41
42 public delegate void SwitchedPlayer();
43 public static event SwitchedPlayer SwitchedEvent;
44
45 private GCPlayer p1 = new GCPlayer(PlayerType.P1);
46 private GCPlayer p2 = new GCPlayer(PlayerType.P2);
47 private GCPlayer currentPlayer;
48
49 private GameState gameState;
50
51 //EXPERIMENT_TIMER
52 public const string PLAYER_TIMER = "PLAYER_TIMER";
53 public const int DEFAULT_PLAYER_TIMER_MIN = 10; //minutes
54 public Text whiteTimerText;
55 public Text blackTimerText;
56
57 private bool whiteTurn;
58 private float whiteTimer;
59 private float blackTimer;
60 //END of EXPERIMENT_TIMER
61
62 private bool ready;
63
64 public GCPlayer PlayerOponent {
65 get {
66 return Opponent(currentPlayer);
67 }
68 }
69
70 public Material HighlightCheckMaterial {
71 get {return highlightCheckMaterial;}
72 }
73
74 public Material HighlightEatMaterial {
75 get {return highlightEatMaterial;}
76 }
77
78 public Color PieceHighlightColor {
79 get {return pieceHighlightColor;}
80 }
81
82 public Material HighlightMoveMaterial {
83 get {return highlightMoveMaterial;}
84 }
85
86 public GameState GameState {
87 get {return gameState;}
88 }
89
90 public GCPlayer CurrentPlayer {
91 get {return currentPlayer;}
92 }
93
94 public LayerMask CLickableMask {
95 get {return clickableMask;}
96 }
97
98 public Camera MainCamera {
99 get {return mainCamera;}
100 }
101
102 public GCPlayer P1 {
103 get {return p1;}
104 }
105
106 public GCPlayer P2 {
107 get {return p2;}
108 }
109
110 public bool IsReady {
111 get {return ready;}
112 }
113
114 public Material PieceP1 {
115 get {return pieceP1;}
116 }
117
118 public Material PieceP2 {
119 get {return pieceP2;}
120 }
121
122 public Grid Grid {
123 get {return grid;}
124 }
125
126 void Awake() {
127 _destroyOnLoad = destroyOnLoad;
128 gameState = new GameState();
129 LoadScores();
130
131 //EXPERIMENT_TIMER
132 SetTimers();
133 }
134
135 //EXPERIMENT_TIMER
136 void SetTimers() {
137 float timer = PlayerPrefs.GetInt(PLAYER_TIMER, DEFAULT_PLAYER_TIMER_MIN); //get in minutes
138 timer *= 60f; //convert to seconds
139 whiteTimer = blackTimer = timer;
140 UpdateWhiteTimer();
141 UpdateBlackTimer();
142 }
143
144 // Use this for initialization
145 void Start () {
146 StartCoroutine(init());
147 }
148
149 IEnumerator init() {
150 Stopwatch timer = new Stopwatch();
151 timer.Start();
152
153 //wait for nodes
154 while (!grid.IsReady) yield return null;
155 //wait for pieces
156 while (!grid.ArePiecesSpawned) yield return null; //wait till pieces are spawned
157 while (!p1.IsReady) yield return null; //wait till all pieces are scaled in
158 while (!p2.IsReady) yield return null;
159 print("Time elapsed: " + timer.ElapsedMilliseconds / 1000.0 + "s");
160 timer.Stop();
161
162 //IMPORTANT
163 p1.ComputePieces();
164 p2.ComputePieces();
165 SwitchPlayer(); //if null current player = p1
166
167 //all objects are now ready
168 ready = true;
169 }
170
171 // Update is called once per frame
172 void Update () {
173
174 #if UNITY_EDITOR
175 if (Input.GetKeyDown(KeyCode.Z)) {
176 GameManager.Instance.GameState.Checkmate();
177 GameOver(p1,GameOverType.CHECKMATE); //TODO delete
178 } else if (Input.GetKeyDown(KeyCode.X)) {
179 GameManager.Instance.GameState.Checkmate();
180 GameOver(p2,GameOverType.CHECKMATE); //TODO delete
181 } else if (Input.GetKeyDown(KeyCode.C)) {
182 GameManager.Instance.GameState.Stalemate();
183 GameOver(p2,GameOverType.STALEMATE);
184 }
185 #endif
186
187 if (!ready) return;
188 if (gameState.IsGameOver) return;
189
190
191 //EXPERIMENT_TIMER
192 if (whiteTurn) {
193 whiteTimer -= Time.deltaTime;
194 if (whiteTimer < 0 ) {
195 whiteTimer = 0;
196 GameManager.Instance.GameState.OutOfTime();
197 GameOver(p2, GameOverType.OUT_OF_TIME);
198 }
199 UpdateWhiteTimer();
200 } else {
201 blackTimer -= Time.deltaTime;
202 if (blackTimer < 0 ) {
203 blackTimer = 0;
204 GameManager.Instance.GameState.OutOfTime();
205 GameOver(p1, GameOverType.OUT_OF_TIME);
206 }
207 UpdateBlackTimer();
208 }
209 }
210
211 //EXPERIMENT_TIMER
212 void UpdateWhiteTimer() {
213 whiteTimerText.text = "WHITE\n" + GetChessTimeFormat(whiteTimer);
214 }
215
216 //EXPERIMENT_TIMER
217 void UpdateBlackTimer() {
218 blackTimerText.text = "BLACK\n" + GetChessTimeFormat(blackTimer);
219 }
220
221 //EXPERIMENT_TIMER
222 string GetChessTimeFormat(float timeInSeconds) {
223 int seconds = (int)(timeInSeconds % 60);
224 int minutes = (int)(timeInSeconds / 60);
225
226 int hours = (int)(minutes / 60);
227 minutes = (int)(minutes % 60);
228 string d2 = "D2";
229 return hours.ToString(d2) + ":" + minutes.ToString(d2) + ":" + seconds.ToString(d2);
230 }
231
232 public void SwitchPlayer() {
233 if (currentPlayer != null) {
234 currentPlayer.DisableInput();
235 }
236
237 if (currentPlayer == p2) {
238 currentPlayer = p1;
239 whiteTurn = true;
240 mainCamera.GetComponent<SwitchAngle>().SwitchCamera(PlayerType.P1);
241 } else if (currentPlayer == p1) {
242 currentPlayer = p2;
243 whiteTurn = false;
244 mainCamera.GetComponent<SwitchAngle>().SwitchCamera(PlayerType.P2);
245 } else {
246 currentPlayer = p1;
247 whiteTurn = true;
248 }
249
250 //IF checkmate
251 if (Rules.HasNoMove()) {
252 InputManager.Instance.UnHighlightTile();
253 if (currentPlayer.IsChecked) {
254 GameManager.Instance.GameState.Checkmate();
255 GameOver(PlayerOponent, GameOverType.CHECKMATE);
256 print("CHECKMATE");
257 } else {
258 GameManager.Instance.GameState.Stalemate();
259 print("STALEMATE");
260 GameOver(currentPlayer, GameOverType.STALEMATE);
261 }
262 }
263
264 currentPlayer.EnableInput();
265 if (SwitchedEvent != null) {
266 SwitchedEvent(); //EXPERIMENTAL
267 }
268
269 print("Turn of: " + currentPlayer.Type); //show on screen
270 }
271
272 public GCPlayer Opponent(GCPlayer player) {
273 if (player == null) return null;
274 if (player == p1) return p2;
275 else return p1;
276 }
277
278 public void GameOver(GCPlayer winner, GameOverType gameOverType) {
279 AddGame();
280 switch (gameOverType) {
281 case GameOverType.CHECKMATE:
282 if (winner == p2) {
283 winnerText.text = "CHECKMATE: BLACK wins";
284 AddScore(PLAYER_BLACK);
285 } else if (winner == p1) {
286 winnerText.text = "CHECKMATE: WHITE wins";
287 AddScore(PLAYER_WHITE);
288 }
289 break;
290 case GameOverType.STALEMATE:
291 winnerText.text = "STALEMATE: It's a tie";
292 break;
293 case GameOverType.OUT_OF_TIME:
294 if (winner == p1) {
295 winnerText.text = "OUT OF TIME: WHITE wins";
296 AddScore(PLAYER_WHITE);
297 } else if (winner == p2) {
298 winnerText.text = "OUT OF TIME: BLACK wins";
299 AddScore(PLAYER_BLACK);
300 }
301 break;
302 }
303 continueButton.SetActive(true);
304 }
305
306 public void End() {
307 continueButton.SetActive(false);
308 winnerButton.SetActive(true);
309 }
310
311 public void AddGame() {
312 int newGame = PlayerPrefs.GetInt(GAME_CURRENT,0) + 1;
313 PlayerPrefs.SetInt(GAME_CURRENT,newGame);
314
315 int maxGame = PlayerPrefs.GetInt(GAME_MAX, 0);
316 if (newGame >= maxGame) {
317 End();
318 }
319 }
320
321 public void AddScore(string playerString) {
322 int newScore = PlayerPrefs.GetInt(playerString,0) + 1;
323 PlayerPrefs.SetInt(playerString,newScore);
324
325 int maxScore = PlayerPrefs.GetInt(SCORE_MAX,0);
326 if (newScore >= maxScore) {
327 End();
328 }
329 }
330
331 public void LoadScores() {
332 int white = PlayerPrefs.GetInt(PLAYER_WHITE,0);
333 int black = PlayerPrefs.GetInt(PLAYER_BLACK,0);
334 whiteScoreText.text = white.ToString();
335 blackScoreText.text = black.ToString();
336
337 int gamesPlayed = PlayerPrefs.GetInt(GAME_CURRENT,0);
338 gamesPlayedText.text = gamesPlayed.ToString();
339 }
340
341 public static void ResetScores() {
342 PlayerPrefs.DeleteKey(PLAYER_WHITE);
343 PlayerPrefs.DeleteKey(PLAYER_BLACK);
344 PlayerPrefs.DeleteKey(GAME_CURRENT);
345 }
346 }
EXPERIMENT_TIMER
public const int DEFAULT_PLAYER_TIMER_MIN = 10; minutes
END of EXPERIMENT_TIMER
EXPERIMENT_TIMER
EXPERIMENT_TIMER
float timer = PlayerPrefs.GetInt(PLAYER_TIMER, DEFAULT_PLAYER_TIMER_MIN); get in minutes
timer *= 60f; convert to seconds
Use this for initialization
wait for nodes
wait for pieces
while (!grid.ArePiecesSpawned) yield return null; wait till pieces are spawned
while (!p1.IsReady) yield return null; wait till all pieces are scaled in
IMPORTANT
SwitchPlayer(); if null current player = p1
all objects are now ready
Update is called once per frame
GameOver(p1,GameOverType.CHECKMATE); TODO delete
GameOver(p2,GameOverType.CHECKMATE); TODO delete
EXPERIMENT_TIMER
EXPERIMENT_TIMER
EXPERIMENT_TIMER
EXPERIMENT_TIMER
IF checkmate
SwitchedEvent(); EXPERIMENTAL
print("Turn of: " + currentPlayer.Type); show on screen